package com.cspg.design.presenter;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TableLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.model.LatLng;
import com.bumptech.glide.Glide;
import com.cspg.design.CheckPermissionsActivity;
import com.cspg.design.MainActivity;
import com.cspg.design.R;
import com.cspg.design.bean.UserInfo;
import com.cspg.design.bean.deviceBean.PointDeviceBean;
import com.cspg.design.bean.deviceBean.PolylineDeviceBean;
import com.cspg.design.common.Constant;
import com.cspg.design.dto.ElectrifiedData;
import com.cspg.design.dto.PointDeviceDto;
import com.cspg.design.dto.PolylineDeviceDto;
import com.cspg.design.dto.ProjectDto;
import com.cspg.design.dto.ProjectFilePath;
import com.cspg.design.dto.ProjectInformation;
import com.cspg.design.dto.ProjectParameter;
import com.cspg.design.dto.ProjectProcessInformation;
import com.cspg.design.model.UserModel;
import com.cspg.design.request.Login;
import com.cspg.design.request.Project;
import com.cspg.design.ui.advise.AdviseActivity;
import com.cspg.design.ui.document.PdfReaderActivity;
import com.cspg.design.ui.gtasks.GTasksActivity;
import com.cspg.design.ui.login.LoginActivity;
import com.cspg.design.utils.Utils;
import com.cspg.design.widget.CSPGProgressBar;
import com.cspg.design.widget.ToastUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.ResponseBody;
import rx.functions.Action1;

import static android.content.ContentValues.TAG;

public class ProjectPresenter implements IPresenter {

    private Project project;
    private Activity activity;

    private CSPGProgressBar cspgProgressBar;

    private ListView listView;
    private SimpleAdapter simpleAdapter;
    private List<HashMap<String ,String >> hashMaps;
    private List<ProjectFilePath> projectFilePathList;

    private String mFileName;
    private ImageView mImageView;

    private TableLayout flowTb;
    private BaiduMap mBaiduMap;
    private LatLng preLatLng;
    private List<ProjectDto> projectDtos;
    private String searchTxt;
    private String projectId;


    public ProjectPresenter(Activity activity) {
        this.project = new Project(this);
        this.activity = activity;
    }

    public ProjectPresenter(AppCompatActivity activity) {
        this.project = new Project(this);
        this.activity = activity;
    }


    public ProjectPresenter(Activity  activity, ListView listView, SimpleAdapter simpleAdapter,List<HashMap<String ,String >> hashMaps)
    {
        this.project = new Project(this);
        this.activity = activity;
        this.listView =listView;
        this.simpleAdapter = simpleAdapter;
        this.hashMaps = hashMaps;
    }


    public void getProjectInformation(String ProjectID) {
//        activity.showProgress();
        if (cspgProgressBar == null) {
            cspgProgressBar = CSPGProgressBar.show(activity);
        } else {
            cspgProgressBar.show();
        }
        try {

            project.getProjectInformation(ProjectID);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getProjectParameter(String ProjectID) {
//        activity.showProgress();
        if (cspgProgressBar == null) {
            cspgProgressBar = CSPGProgressBar.show(activity);
        } else {
            cspgProgressBar.show();
        }
        try {

            project.getGetProjectTerrain(ProjectID);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getProjectFilePath(String ProjectID) {
//        activity.showProgress();
        if (cspgProgressBar == null) {
            cspgProgressBar = CSPGProgressBar.show(activity);
        } else {
            cspgProgressBar.show();
        }
        try {

            project.getGetProjectFilePath(ProjectID,UserInfo.getInstance().UserRight);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void getProjectFile(String projectId,String FilePath, String fileName , ImageView imageView) {
//        activity.showProgress();
        if (cspgProgressBar == null) {
            cspgProgressBar = CSPGProgressBar.show(activity);
        } else {
            cspgProgressBar.show();
        }
        try {
            this.mFileName = fileName;
            this.mImageView = imageView;
            this.projectId = projectId;
            String contentType = "application/json;charset=UTF-8";
            project.getGetProjectFile(FilePath,contentType);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getProjectSubmit(String ProjectID, String EmployeeID, boolean Result , String Remark ) {
//        activity.showProgress();
        if (cspgProgressBar == null) {
            cspgProgressBar = CSPGProgressBar.show(activity);
        } else {
            cspgProgressBar.show();
        }
        try {
            project.getProjectSubmit( ProjectID,  EmployeeID,  Result ,  Remark );
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getProjectFlow(String ProjectID, TableLayout tableLayout) {
//        activity.showProgress();
        if (cspgProgressBar == null) {
            cspgProgressBar = CSPGProgressBar.show(activity);
        } else {
            cspgProgressBar.show();
        }
        try {
            this.flowTb = tableLayout;
            project.getProjectFlow( ProjectID);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getProjectDevice(String projectID, BaiduMap baiduMap)
    {
        if (cspgProgressBar == null) {
            cspgProgressBar = CSPGProgressBar.show(activity);
        } else {
            cspgProgressBar.show();
        }
        try {
            this.mBaiduMap = baiduMap;
            project.getProjectDevice(projectID);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getProject( String searchTxt )
    {
        if (cspgProgressBar == null) {
            cspgProgressBar = CSPGProgressBar.show(activity);
        } else {
            cspgProgressBar.show();
        }
        try {
            if (searchTxt != null && !searchTxt.isEmpty())
            {
                this.searchTxt = searchTxt;
            }
            project.getProject(UserInfo.getInstance().EmployeeID);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getProjectHistory(String searchTxt)
    {
        if (cspgProgressBar == null) {
            cspgProgressBar = CSPGProgressBar.show(activity);
        } else {
            cspgProgressBar.show();
        }
        try {
            project.getProjectHistory(UserInfo.getInstance().EmployeeID);
            this.searchTxt = searchTxt;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void postProjectElectrifiedSubmit(String projectID, String electrifiedData)
    {
        if (cspgProgressBar == null) {
            cspgProgressBar = CSPGProgressBar.show(activity);
        } else {
            cspgProgressBar.show();
        }
        try {
            project.postProjectElectrifiedSubmit(projectID, electrifiedData);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void netWorkError(String netError) {

        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }
        ToastUtils.toast(activity, netError);
        if (activity instanceof MainActivity) {
            //activity.openNewActivity(LoginActivity.class);
            Intent intent = new Intent();
            intent.setClass(activity, LoginActivity.class);
            activity.startActivity(intent);
        }
    }

    @Override
    public void httpRequestFailure(String noDataError) {
        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }
        ToastUtils.toast(activity, noDataError);
        if (activity instanceof MainActivity) {
            //activity.openNewActivity(LoginActivity.class);
            Intent intent = new Intent();
            intent.setClass(activity, LoginActivity.class);
            activity.startActivity(intent);
        }
    }

    @Override
    public void httpRequestSuccess() {
        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }
        ToastUtils.toast(activity, "成功。。。。");
        this.activity.startActivity(new Intent(activity, GTasksActivity.class));
        activity.finish();
    }

    public void getTokenExpireFailure() {

        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }
        Intent intent = new Intent();
        intent.setClass(activity, LoginActivity.class);
        activity.startActivity(intent);
    }

    public void projectRequestSuccess(List<ProjectDto> projectDtos)
    {
        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }
        refreshListView(projectDtos);
        this.projectDtos = projectDtos;
    }
    private void refreshListView(List<ProjectDto> projectDtos) {
        hashMaps.clear();
        for (int i = 0; i < projectDtos.size(); i++) {
            ProjectDto projectDto  = projectDtos.get(i);
            if (this.searchTxt != null && !this.searchTxt.isEmpty() )
            {
                if (projectDto.getProjectName().contains(this.searchTxt))
                {
                    HashMap<String, String> hashMap = new HashMap<String, String>();
                    hashMap.put("title", projectDtos.get(i).getProjectName());
                    hashMap.put("name",projectDtos.get(i).getProjectID());
                    hashMap.put("status", "待审核");
                    hashMaps.add(hashMap);
                }
            }
            else
            {
                HashMap<String, String> hashMap = new HashMap<String, String>();
                hashMap.put("title", projectDtos.get(i).getProjectName());
                hashMap.put("name",projectDtos.get(i).getProjectID());
                hashMap.put("status", "待审核");
                hashMaps.add(hashMap);
            }
        }
        simpleAdapter.notifyDataSetChanged();
    }

    public void projectHistoryRequestSuccess(List<ProjectDto> projectDtos)
    {
        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
                cspgProgressBar.dismiss();
        }
        List<ProjectDto> list = new ArrayList<>();
        hashMaps.clear();
        for (int i = 0; i < projectDtos.size(); i++) {
            ProjectDto projectDto  = projectDtos.get(i);
            if (this.searchTxt != null && !this.searchTxt.isEmpty() )
            {
                if (projectDto.getProjectName().contains(this.searchTxt))
                {
                    HashMap<String, String> hashMap = new HashMap<String, String>();
                    hashMap.put("title", projectDtos.get(i).getProjectName());
                    hashMap.put("name",projectDtos.get(i).getProjectID());
                    if (projectDtos.get(i).getIsOk().equals("false"))
                    {
                        hashMap.put("status", "审核中");
                    }
                    else{
                        hashMap.put("status", "审核完成");
                    }
                    hashMaps.add(hashMap);
                    list.add(projectDto);
                }
            }
            else
            {
                HashMap<String, String> hashMap = new HashMap<String, String>();
                hashMap.put("title", projectDtos.get(i).getProjectName());
                hashMap.put("name",projectDtos.get(i).getProjectID());
                if (projectDtos.get(i).getIsOk().equals("false"))
                {
                    hashMap.put("status", "审核中");
                }
                else{
                    hashMap.put("status", "审核完成");
                }
                hashMaps.add(hashMap);
                list.add(projectDto);
            }
        }
        this.projectDtos = list;
        simpleAdapter.notifyDataSetChanged();
    }

    public void projectInfoHttpRequestSuccess(ProjectInformation projectInformation) {

        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }

        TextView tvProjectName = activity.findViewById(R.id.tv_project_info_project_name);
        TextView tvProjectCode = activity.findViewById(R.id.tv_project_info_project_code);
        TextView tvProjectType = activity.findViewById(R.id.tv_project_info_project_type);
        TextView tvProjectInvestment = activity.findViewById(R.id.tv_project_info_project_investment);
        TextView tvProjectVoltageLevel = activity.findViewById(R.id.tv_project_info_voltage_level);
        TextView tvProjectUpperUnit = activity.findViewById(R.id.tv_project_info_supper_unit);
        TextView tvProjectDesignUnit = activity.findViewById(R.id.tv_project_info_design_unit);
        TextView tvProjectLiaison = activity.findViewById(R.id.tv_project_info_liaison);
        tvProjectName.setText(projectInformation.getProjectName());
        ;
        tvProjectCode.setText(projectInformation.getPlanNum());
        tvProjectType.setText(projectInformation.getProjectType());
        tvProjectInvestment.setText(projectInformation.getInvestment());
        tvProjectVoltageLevel.setText(projectInformation.getVoltageLevel());
        tvProjectUpperUnit.setText(projectInformation.getOwneCountiarea());
        tvProjectDesignUnit.setText(projectInformation.getDesignerName());
        tvProjectLiaison.setText(projectInformation.getLiaison());

    }

    public void projectParameterHttpRequestSuccess(ProjectParameter projectParameter) {

        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }
        TextView tvProjectCarDistance = activity.findViewById(R.id.tv_project_parameter_car_distance);
        TextView tvProjectHandDistance = activity.findViewById(R.id.tv_project_parameter_hand_distance);
        TextView tvProjectFlatGround = activity.findViewById(R.id.tv_project_parameter_flat_ground);
        TextView tvProjectHillyArea = activity.findViewById(R.id.tv_project_parameter_hilly_area);
        TextView tvProjectHills = activity.findViewById(R.id.tv_project_parameter_hills);
        TextView tvProjectCommonSoil = activity.findViewById(R.id.tv_project_parameter_common_soil);
        TextView tvProjectHandSoil = activity.findViewById(R.id.tv_project_parameter_hard_soil);
        TextView tvProjectLooseSandstone= activity.findViewById(R.id.tv_project_parameter_loose_sandstone);
        TextView tvProjectRock = activity.findViewById(R.id.tv_project_parameter_rock);
        TextView tvProjectMudPuddle = activity.findViewById(R.id.tv_project_parameter_mud_puddle);

        tvProjectCarDistance.setText(projectParameter.getCarDistance());
        tvProjectHandDistance.setText(projectParameter.getManDistance());
        tvProjectFlatGround.setText(projectParameter.getPD());
        tvProjectHillyArea.setText(projectParameter.getSD());
        tvProjectHills.setText(projectParameter.getQL());
        tvProjectCommonSoil.setText(projectParameter.getPTT());
        tvProjectHandSoil.setText(projectParameter.getJT());
        tvProjectLooseSandstone.setText(projectParameter.getSSS());
        tvProjectRock.setText(projectParameter.getYSRG());
        tvProjectMudPuddle.setText(projectParameter.getSNK());

    }

    public void projectFilePathHttpRequestSuccess(List<ProjectFilePath> projectFilePathList) {

        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }
        initProjectFilePathUI(projectFilePathList);
    }
    private void initProjectFilePathUI(List<ProjectFilePath> projectFilePathList)
    {
        this.projectFilePathList = projectFilePathList;
        String key = "file";
        for (int i =0 ;i< projectFilePathList.size();i++)
        {
            ProjectFilePath projectFilePath = projectFilePathList.get(i);
            HashMap<String,String> hashMap = new HashMap<>();
            hashMap.put(key,projectFilePath.getFileName());
            hashMaps.add(hashMap);
        }
        simpleAdapter.notifyDataSetChanged();
    }

    public void projectFileHttpRequestSuccess(ResponseBody responseBody)
    {
        writeResponseBodyToDisk(responseBody);
    }

    public List<ProjectFilePath> getProjectFilePath( )
    {
        return this.projectFilePathList;
    }

    private boolean writeResponseBodyToDisk(ResponseBody body) {
        try {
            // todo change the file location/name according to your needs
            File futureStudioDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) +"/"+projectId);
            if (!futureStudioDir.exists()){
                futureStudioDir.mkdirs();
            }

            File futureStudioIconFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) +"/"+projectId, mFileName);
            if (futureStudioIconFile.exists())
            {
                futureStudioIconFile.delete();
                futureStudioIconFile.createNewFile();
            }else {
                futureStudioIconFile.createNewFile();
            }

            InputStream inputStream = null;
            OutputStream outputStream = null;

            try {
                byte[] fileReader = new byte[4096];

                long fileSize = body.contentLength();
                long fileSizeDownloaded = 0;

                inputStream = body.byteStream();
                outputStream = new FileOutputStream(futureStudioIconFile);

                while (true) {
                    int read = inputStream.read(fileReader);

                    if (read == -1) {
                        break;
                    }

                    outputStream.write(fileReader, 0, read);

                    fileSizeDownloaded += read;

                    Log.d(TAG, "file download: " + fileSizeDownloaded + " of " + fileSize);
                }

                outputStream.flush();

                return true;
            } catch (IOException e) {
                return false;
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }

                if (outputStream != null) {
                    outputStream.close();
                    if (mImageView != null)
                    {
                        if (futureStudioIconFile.exists())
                        {
                            System.out.println("///地址：////"+futureStudioIconFile);
                            Glide.with(this.activity)
                                    .load(futureStudioIconFile)
                                    .placeholder(R.drawable.ic_add_img)
                                    .error(R.drawable.ic_error_image)
                                    .into(mImageView);
                        }

                    }
                    else if (Utils.getPrefix(futureStudioIconFile).contains("pdf"))
                    {
                        Intent intent = new Intent(this.activity, PdfReaderActivity.class);
                        intent.putExtra("file", futureStudioIconFile.getAbsolutePath());
                        this.activity.startActivity(intent);
                    }
                    else
                    {

                    }
                    if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
                        cspgProgressBar.dismiss();
                    }
                }
            }
        } catch (IOException e) {
            return false;
        }
    }
    public  void projectFlowHttpRequestSuccess(List<ProjectProcessInformation> projectProcessInformationList)
    {
        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }
        for (int i = 0 ; i< projectProcessInformationList.size();i++)
        {
            ProjectProcessInformation projectProcessInformation = projectProcessInformationList.get(i);
            View view = LayoutInflater.from(this.activity).inflate(R.layout.layout_flow_item,null);
            TextView orderEt = view.findViewById(R.id.flow_item_order);
            Integer order = i+1;
            orderEt.setText(order.toString());
            TextView reviewManEt = view.findViewById(R.id.flow_item_review_man);
            reviewManEt.setText(projectProcessInformation.getEmployeeName());
            TextView unitET = view.findViewById(R.id.flow_item_unit);
            unitET.setText(projectProcessInformation.getCompanyName());
            TextView contactEt = view.findViewById(R.id.flow_item_contact);
            contactEt.setText(projectProcessInformation.getEmployeePhoto());
            TextView timeEt = view.findViewById(R.id.flow_item_review_time);
            timeEt.setText(projectProcessInformation.getTimeComment());
            this.flowTb.addView(view);
        }
    }
    public void getFailure(String msg) {

        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }
        ToastUtils.toast(activity, msg);
    }

    public void requestSuccessPolylines(List<PolylineDeviceDto> polylineDeviceDtoLis, int type) {
        if (polylineDeviceDtoLis != null) {
            processDataPolylines(polylineDeviceDtoLis,type);
        }
    }

    public void requestSuccessPoints(List<PointDeviceDto> pointDeviceDtoList , int type) {
        if (pointDeviceDtoList != null) {
            processDataPoints(pointDeviceDtoList,type);
        }
    }

    // type : 0  设备上传 1 设备按项目下载
    private void processDataPoints(List<PointDeviceDto> pointDeviceDtos , int type )
    {
        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }
        if (type == 0)
        {
            ToastUtils.toast(activity,"点设备上传成功。。。");
        }
        else if (type == 1)
        {
            initPoint(pointDeviceDtosConverterFactoryToBeans(pointDeviceDtos));
        }
    }
    // type : 0  设备上传 1 设备按项目下载
    private void processDataPolylines(List<PolylineDeviceDto> polylineDeviceDtos, int type )
    {
        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }
        if (type == 0)
        {
            ToastUtils.toast(activity,"导线上传成功。。。");
        }
        else if (type == 1)
        {
            if (polylineDeviceDtos.size()>0)
            {
                initPolyline(polylineDeviceDtosConverterFactoryToBeans(polylineDeviceDtos));
            }
        }
    }

    //初始化点
    private void initPoint(List<PointDeviceBean> pointDeviceBeans) {
        for (int i = 0; i < pointDeviceBeans.size(); i++) {
            PointDeviceBean pointDeviceBean = pointDeviceBeans.get(i);
            LatLng latLng = new LatLng(pointDeviceBean.getLatitude(), pointDeviceBean.getLongitude());
            addPoint(latLng, pointDeviceBean.getPointUuid(),pointDeviceBean.getPointName(),getResourceId(pointDeviceBean.getPointType()));
            if (i == 0) {
                MapStatus.Builder builder = new MapStatus.Builder();
                builder.target(latLng).zoom(18.0f);
                mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
            }
        }
    }

    //初始化线
    private void initPolyline(List<PolylineDeviceBean> polylineDeviceBeans) {
        for (int i = 0; i < polylineDeviceBeans.size(); i++) {
            PolylineDeviceBean polylineDeviceBean = polylineDeviceBeans.get(i);
            preLatLng = new LatLng(polylineDeviceBean.getStartLatitude(), polylineDeviceBean.getStartLongitude());
            addPolyline(new LatLng(polylineDeviceBean.getEndLatitude(), polylineDeviceBean.getEndLongitude()), polylineDeviceBean.getPolylineUuid());
        }

    }

    //添加点设备
    private void addPoint(LatLng latLng, String uuid, String device_name, int device_type) {
        View marker_temp = LayoutInflater.from(this.activity.getApplicationContext()).inflate(R.layout.marker_text_up_img_down, null);
        TextView marker_tv_temp = (TextView) marker_temp.findViewById(R.id.baidumap_custom_text);
        ImageView marker_img_temp = (ImageView) marker_temp.findViewById(R.id.baidumap_custom_img);
        marker_tv_temp.setText(device_name);
        marker_img_temp.setImageResource(device_type);
        Bundle bundle = new Bundle();
        bundle.putString("uuid", uuid);
        BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromView(marker_temp);
        OverlayOptions options = new MarkerOptions()
                .position(latLng)
                .icon(bitmapDescriptor)
                .title(device_name)
                .draggable(true)
                .yOffset(45)
                .extraInfo(bundle);
        mBaiduMap.addOverlay(options);
    }
    //添加线设备
    private void addPolyline(LatLng latLng, String uuid) {
        if (preLatLng == null) {
            preLatLng = latLng;
        } else {
            Bundle bundle = new Bundle();
            bundle.putString("uuid", uuid);
            List<LatLng> points = new ArrayList<>();
            points.add(preLatLng);
            points.add(latLng);
            preLatLng = latLng;
            OverlayOptions mOverLayerOptions = new PolylineOptions().width(5).color(Color.RED).extraInfo(bundle).points(points);
            mBaiduMap.addOverlay(mOverLayerOptions);
        }
    }

    //通过设备类型获取设备资源id
    private int getResourceId(int type) {
        //设备类型 1：杆塔 2： 搭火点  3： 变压器 4 ：用户点 5 ：门铁
        int id = 0;
        switch (type) {
            case 1:
                id = R.drawable.ic_tower;
                break;
            case 2:
                id = R.drawable.ic_setting_fire;
                break;
            case 3:
                id = R.drawable.ic_trasform;
                break;
            case 4:
                id = R.drawable.ic_end_point;
                break;
            case 5:
                id = R.drawable.ic_door;
                break;
        }
        return id;
    }

    private List<PointDeviceBean> pointDeviceDtosConverterFactoryToBeans(List<PointDeviceDto> pointDeviceDtos)
    {
        List<PointDeviceBean> pointDeviceBeans = new ArrayList<>();
        for (int i=0;i<pointDeviceDtos.size();i++)
        {
            pointDeviceBeans.add(pointDeviceDtoConverterFactoryToBean(pointDeviceDtos.get(i)));
        }
        return pointDeviceBeans;
    }


    private PointDeviceBean pointDeviceDtoConverterFactoryToBean(PointDeviceDto pointDeviceDto)
    {
        PointDeviceBean pointDeviceBean = new PointDeviceBean();
        pointDeviceBean.setPointId(pointDeviceDto.getId());
        pointDeviceBean.setProjectId( pointDeviceDto.getProjectId());
        pointDeviceBean.setPointName( pointDeviceDto.getPointName());
        pointDeviceBean.setPointType( pointDeviceDto.getPointType());
        pointDeviceBean.setPointUuid( pointDeviceDto.getPointUuid());
        pointDeviceBean.setPointModel( pointDeviceDto.getPointModel());
        pointDeviceBean.setLatitude( pointDeviceDto.getLatitude());
        pointDeviceBean.setLongitude( pointDeviceDto.getLongitude());
        pointDeviceBean.setPrePointUUID( pointDeviceDto.getPrePointUUID());
        pointDeviceBean.setPointStatue( pointDeviceDto.getPointStatue());
        pointDeviceBean.setSchemaName( pointDeviceDto.getSchemaName());
        pointDeviceBean.setDistance( pointDeviceDto.getDistance());
        return pointDeviceBean;
    }
    private List<PointDeviceDto> pointDeviceBeansConverterFactoryToDtos(List<PointDeviceBean> pointDeviceBeans)
    {
        List<PointDeviceDto> pointDeviceDtos = new ArrayList<>();
        for (int i=0;i<pointDeviceBeans.size();i++)
        {
            pointDeviceDtos.add(pointDeviceBeanConverterFactoryToDto(pointDeviceBeans.get(i)));
        }
        return pointDeviceDtos;
    }

    private PointDeviceDto pointDeviceBeanConverterFactoryToDto(PointDeviceBean pointDeviceBean)
    {
        PointDeviceDto pointDeviceDto = new PointDeviceDto();
//        pointDeviceDto.setId( pointDeviceBean.getPointId());
        pointDeviceDto.setPointId(pointDeviceBean.getPointId());
        pointDeviceDto.setProjectId( pointDeviceBean.getProjectId());
        pointDeviceDto.setPointName( pointDeviceBean.getPointName());
        pointDeviceDto.setPointType( pointDeviceBean.getPointType());
        pointDeviceDto.setPointUuid( pointDeviceBean.getPointUuid());
        pointDeviceDto.setPointModel( pointDeviceBean.getPointModel());
        pointDeviceDto.setLatitude( pointDeviceBean.getLatitude());
        pointDeviceDto.setLongitude( pointDeviceBean.getLongitude());
        pointDeviceDto.setPrePointUUID( pointDeviceBean.getPrePointUUID());
        pointDeviceDto.setPointStatue( pointDeviceBean.getPointStatue());
        pointDeviceDto.setSchemaName( pointDeviceBean.getSchemaName());
        pointDeviceDto.setDistance( pointDeviceBean.getDistance());
        return pointDeviceDto;
    }

    private List<PolylineDeviceBean> polylineDeviceDtosConverterFactoryToBeans(List<PolylineDeviceDto> polylineDeviceDtos)
    {
        List<PolylineDeviceBean> polylineDeviceBeans = new ArrayList<>();
        for (int i=0;i<polylineDeviceDtos.size();i++)
        {
            polylineDeviceBeans.add(polylineDeviceDtoConverterFactoryToBean(polylineDeviceDtos.get(i)));
        }
        return polylineDeviceBeans;
    }


    private PolylineDeviceBean polylineDeviceDtoConverterFactoryToBean(PolylineDeviceDto polylineDeviceDto)
    {
        PolylineDeviceBean polylineDeviceBean = new PolylineDeviceBean();

        polylineDeviceBean.setPolyLineId( polylineDeviceDto.getId());
        polylineDeviceBean.setProjectId( polylineDeviceDto.getProjectId());
        polylineDeviceBean.setStartPointUUID( polylineDeviceDto.getStartPointUUID());
        polylineDeviceBean.setEndPointUUID( polylineDeviceDto.getEndPointUUID());
        polylineDeviceBean.setStartLatitude( polylineDeviceDto.getStartLatitude());
        polylineDeviceBean.setStartLongitude( polylineDeviceDto.getStartLongitude());
        polylineDeviceBean.setEndLatitude( polylineDeviceDto.getEndLatitude());
        polylineDeviceBean.setEndLongitude( polylineDeviceDto.getEndLongitude());
        polylineDeviceBean.setPolylineUuid( polylineDeviceDto.getPolylineUuid());
        polylineDeviceBean.setPolylineModel( polylineDeviceDto.getPolylineModel());
        polylineDeviceBean.setStartPointName( polylineDeviceDto.getStartPointName());
        polylineDeviceBean.setEndPointName( polylineDeviceDto.getEndPointName());
        polylineDeviceBean.setCircuitsNum( polylineDeviceDto.getCircuitsNum());
        polylineDeviceBean.setMargin( polylineDeviceDto.getMargin());
        return polylineDeviceBean;
    }
    private List<PolylineDeviceDto> polylineDeviceBeansConverterFactoryToDtos(List<PolylineDeviceBean> polylineDeviceBeans)
    {
        List<PolylineDeviceDto> polylineDeviceDtos = new ArrayList<>();
        for (int i=0;i<polylineDeviceBeans.size();i++)
        {
            polylineDeviceDtos.add(polylineDeviceBeanConverterFactoryToDto(polylineDeviceBeans.get(i)));
        }
        return polylineDeviceDtos;
    }

    private PolylineDeviceDto polylineDeviceBeanConverterFactoryToDto(PolylineDeviceBean polylineDeviceBean)
    {
        PolylineDeviceDto polylineDeviceDto = new PolylineDeviceDto();

        polylineDeviceDto.setId( polylineDeviceBean.getPolyLineId());
        polylineDeviceDto.setProjectId( polylineDeviceBean.getProjectId());
        polylineDeviceDto.setStartPointUUID( polylineDeviceBean.getStartPointUUID());
        polylineDeviceDto.setEndPointUUID( polylineDeviceBean.getEndPointUUID());
        polylineDeviceDto.setStartLatitude( polylineDeviceBean.getStartLatitude());
        polylineDeviceDto.setStartLongitude( polylineDeviceBean.getStartLongitude());
        polylineDeviceDto.setEndLatitude( polylineDeviceBean.getEndLatitude());
        polylineDeviceDto.setEndLongitude( polylineDeviceBean.getEndLongitude());
        polylineDeviceDto.setPolylineUuid( polylineDeviceBean.getPolylineUuid());
        polylineDeviceDto.setPolylineModel( polylineDeviceBean.getPolylineModel());
        polylineDeviceDto.setStartPointName( polylineDeviceBean.getStartPointName());
        polylineDeviceDto.setEndPointName( polylineDeviceBean.getEndPointName());
        polylineDeviceDto.setCircuitsNum( polylineDeviceBean.getCircuitsNum());
        polylineDeviceDto.setMargin( polylineDeviceBean.getMargin());
        return polylineDeviceDto;
    }

    public List<ProjectDto> getProjectDto()
    {
        return  this.projectDtos;
    }
}